package chucnang;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.table.DefaultTableModel;

import check_form.CheckString;

import com.toedter.calendar.JDateChooser;

import form.DSJ_DieuKhoanThanhToan;
import form.DSJ_FormMuaHang;
import form.DSJ_NPPMuaHang;
import form.DSJ_NhanVien;
import form.FormMuaHang;
import form.FormUpdateMuaHang;

import trangchu.TrangChu;
import trangchu.UtilDelegate;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JCheckBox;


public class MuaHang extends JPanel implements ActionListener {

    private JPanel plNorth,plCenter,plSouth;
    public JComboBox cmbTenNPP;
    public JTextField txtDiaChi;
	public JComboBox cmbMa;
	public JTextField txtDienThoai;
	public JComboBox cmbTenNV;
	public JComboBox cmbThanhToan;
	public JTextField txtTong,txtDaTra,txtConLai;
	public JButton btnThem,btnSua,btnXoa,btnTaoMoi;
	public  JTable table;
	public DefaultTableModel model;
	public JDateChooser dateStart;
	public JDateChooser datehanthanhtoan;
	private JLabel lblTenNPP;
	 
	Object[][] data = {};
    String[] colunms = {"Mã Hàng","Tên Hàng","Kho Hàng","Đơn Vị","Đơn Giá","Số Lượng","Thành Tiền","ID"};
    ResultSet rs;
	PreparedStatement st;
	Connection con =UtilDelegate.getConnection();
	private int row;
	private TrangChu trangchu;
	private FormUpdateMuaHang updatemuahang;
	private FormMuaHang formmuahang;
	private MenuAction_MuaHang pMenu;
	public TitledBorder title;
	private JLabel lblGhiChu;
	public JTextField txtGhiChu;
	private JLabel lblPhieu;
	public JTextField txtPhieu;
	private MuaHang frame;
	private JCheckBox cbXacNhanPP;
	private JLabel lblsao;
	public MuaHang(TrangChu trangchu) {
    	this.trangchu = trangchu;    	
    	this.setLayout(new BorderLayout(15,15));
    	plNorth = new JPanel();
		add(plNorth,BorderLayout.NORTH);
		GridBagLayout gbl_plNorth = new GridBagLayout();
		gbl_plNorth.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_plNorth.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_plNorth.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_plNorth.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		plNorth.setLayout(gbl_plNorth);
		
		//JLabel lblThongTin = new JLabel("Thông tin nhập hàng");
		title = new TitledBorder("Nhập Hàng");
		plNorth.setBorder(title);
		GridBagConstraints gbc_lblThongTin = new GridBagConstraints();
		gbc_lblThongTin.insets = new Insets(0, 0, 5, 5);
		gbc_lblThongTin.gridx = 0;
		gbc_lblThongTin.gridy = 1;
		//plNorth.add(lblThongTin, gbc_lblThongTin);
		
		lblTenNPP = new JLabel("Tên Nhà Phân Phối: ");
		GridBagConstraints gbc_lblTenNPP = new GridBagConstraints();
		gbc_lblTenNPP.anchor = GridBagConstraints.WEST;
		gbc_lblTenNPP.insets = new Insets(0, 0, 5, 5);
		gbc_lblTenNPP.gridx = 1;
		gbc_lblTenNPP.gridy = 2;
		plNorth.add(lblTenNPP, gbc_lblTenNPP);
		
		cmbTenNPP = new JComboBox();
		GridBagConstraints gbc_cmbTenNPP = new GridBagConstraints();
		gbc_cmbTenNPP.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbTenNPP.gridwidth = 18;
		gbc_cmbTenNPP.insets = new Insets(0, 0, 5, 5);
		gbc_cmbTenNPP.gridx = 2;
		gbc_cmbTenNPP.gridy = 2;
		plNorth.add(cmbTenNPP, gbc_cmbTenNPP);
		
		
		JLabel lblMa = new JLabel("Mã: ");
		GridBagConstraints gbc_lblMa = new GridBagConstraints();
		gbc_lblMa.anchor = GridBagConstraints.WEST;
		gbc_lblMa.insets = new Insets(0, 0, 5, 5);
		gbc_lblMa.gridx = 21;
		gbc_lblMa.gridy = 2;
		plNorth.add(lblMa, gbc_lblMa);
		
		cmbMa = new JComboBox();
		GridBagConstraints gbc_cmbMa = new GridBagConstraints();
		gbc_cmbMa.gridwidth = 1;
		gbc_cmbMa.insets = new Insets(0, 0, 5, 5);
		gbc_cmbMa.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbMa.gridx = 22;
		gbc_cmbMa.gridy = 2;
		plNorth.add(cmbMa, gbc_cmbMa);
		
		JLabel lblDiaChi = new JLabel("Địa Chỉ: ");
		GridBagConstraints gbc_lblDiaChi = new GridBagConstraints();
		gbc_lblDiaChi.anchor = GridBagConstraints.WEST;
		gbc_lblDiaChi.insets = new Insets(0, 0, 5, 5);
		gbc_lblDiaChi.gridx = 1;
		gbc_lblDiaChi.gridy = 3;
		plNorth.add(lblDiaChi, gbc_lblDiaChi);
		
		txtDiaChi = new JTextField();
		txtDiaChi.setEditable(false);
		GridBagConstraints gbc_txtDiaChi = new GridBagConstraints();
		gbc_txtDiaChi.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDiaChi.gridwidth = 18;
		gbc_txtDiaChi.insets = new Insets(0, 0, 5, 5);
		gbc_txtDiaChi.gridx = 2;
		gbc_txtDiaChi.gridy = 3;
		plNorth.add(txtDiaChi, gbc_txtDiaChi);
		txtDiaChi.setColumns(10);
		
		JLabel lblDienThoai = new JLabel("Điện Thoại: ");
		GridBagConstraints gbc_lblDienThoai = new GridBagConstraints();
		gbc_lblDienThoai.anchor = GridBagConstraints.WEST;
		gbc_lblDienThoai.insets = new Insets(0, 0, 5, 5);
		gbc_lblDienThoai.gridx = 21;
		gbc_lblDienThoai.gridy = 3;
		plNorth.add(lblDienThoai, gbc_lblDienThoai);
		
		txtDienThoai = new JTextField();
		txtDienThoai.setEditable(false);
		GridBagConstraints gbc_txtDienThoai = new GridBagConstraints();
		gbc_txtDienThoai.gridwidth = 1;
		gbc_txtDienThoai.insets = new Insets(0, 0, 5, 5);
		gbc_txtDienThoai.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDienThoai.gridx = 22;
		gbc_txtDienThoai.gridy = 3;
		plNorth.add(txtDienThoai, gbc_txtDienThoai);
		txtDienThoai.setColumns(10);
		
		JLabel lblTenNN = new JLabel("Nhân Viên Nhận Hàng: ");
		GridBagConstraints gbc_lblTenNN = new GridBagConstraints();
		gbc_lblTenNN.anchor = GridBagConstraints.WEST;
		gbc_lblTenNN.insets = new Insets(0, 0, 5, 5);
		gbc_lblTenNN.gridx = 1;
		gbc_lblTenNN.gridy = 4;
		plNorth.add(lblTenNN, gbc_lblTenNN);
		
		cmbTenNV = new JComboBox();
		GridBagConstraints gbc_cmbTenNV = new GridBagConstraints();
		gbc_cmbTenNV.gridwidth = 18;
		gbc_cmbTenNV.insets = new Insets(0, 0, 5, 5);
		gbc_cmbTenNV.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbTenNV.gridx = 2;
		gbc_cmbTenNV.gridy = 4;
		plNorth.add(cmbTenNV, gbc_cmbTenNV);
		
		JLabel lblNgay = new JLabel("Ngày: ");
		GridBagConstraints gbc_lblNgay = new GridBagConstraints();
		gbc_lblNgay.anchor = GridBagConstraints.WEST;
		gbc_lblNgay.insets = new Insets(0, 0, 5, 5);
		gbc_lblNgay.gridx = 21;
		gbc_lblNgay.gridy = 4;
		plNorth.add(lblNgay, gbc_lblNgay);
			
		GridBagConstraints gbc_txtNgay = new GridBagConstraints();
		gbc_txtNgay.gridwidth = 1;
		gbc_txtNgay.insets = new Insets(0, 0, 5, 5);
		gbc_txtNgay.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtNgay.gridx = 22;
		gbc_txtNgay.gridy = 4;
		dateStart = new JDateChooser();
		dateStart.setDateFormatString("dd/MM/yyyy");
		Date todayD=new Date(System.currentTimeMillis());
		dateStart.setDate(todayD);		
		plNorth.add(dateStart,gbc_txtNgay);
		
		cbXacNhanPP = new JCheckBox("Xác nhận nhà phân phối");
		GridBagConstraints gbc_cbXacNhanPP = new GridBagConstraints();
		gbc_cbXacNhanPP.anchor = GridBagConstraints.EAST;
		gbc_cbXacNhanPP.insets = new Insets(0, 0, 5, 5);
		gbc_cbXacNhanPP.gridx = 23;
		gbc_cbXacNhanPP.gridy = 4;
		plNorth.add(cbXacNhanPP, gbc_cbXacNhanPP);
		
		lblsao = new JLabel("(*)");
		lblsao.setForeground(Color.RED);
		GridBagConstraints gbc_lblsao = new GridBagConstraints();
		gbc_lblsao.anchor = GridBagConstraints.WEST;
		gbc_lblsao.insets = new Insets(0, 0, 5, 5);
		gbc_lblsao.gridx = 24;
		gbc_lblsao.gridy = 4;
		plNorth.add(lblsao, gbc_lblsao);
		
		JLabel lblThanhToan = new JLabel("Điều Khoản Thanh Toán:");
		GridBagConstraints gbc_lblThanhToan = new GridBagConstraints();
		gbc_lblThanhToan.anchor = GridBagConstraints.WEST;
		gbc_lblThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_lblThanhToan.gridx = 1;
		gbc_lblThanhToan.gridy = 5;
		plNorth.add(lblThanhToan, gbc_lblThanhToan);
		
		cmbThanhToan = new JComboBox();
		GridBagConstraints gbc_cmbThanhToan = new GridBagConstraints();
		gbc_cmbThanhToan.gridwidth = 18;
		gbc_cmbThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_cmbThanhToan.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbThanhToan.gridx = 2;
		gbc_cmbThanhToan.gridy = 5;
		plNorth.add(cmbThanhToan, gbc_cmbThanhToan);
		
		JLabel lblHanThanhToan = new JLabel("Hạn Thanh Toán:");
		GridBagConstraints gbc_lblHanThanhToan = new GridBagConstraints();
		gbc_lblHanThanhToan.anchor = GridBagConstraints.WEST;
		gbc_lblHanThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_lblHanThanhToan.gridx = 21;
		gbc_lblHanThanhToan.gridy = 5;
		plNorth.add(lblHanThanhToan, gbc_lblHanThanhToan);
		
		GridBagConstraints gbc_txtHanThanhToan = new GridBagConstraints();
		gbc_txtHanThanhToan.gridwidth = 1;
		gbc_txtHanThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_txtHanThanhToan.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtHanThanhToan.gridx = 22;
		gbc_txtHanThanhToan.gridy = 5;
		datehanthanhtoan = new JDateChooser();
		datehanthanhtoan.setDateFormatString("dd/MM/yyyy");
		plNorth.add(datehanthanhtoan,gbc_txtHanThanhToan);
		
		lblGhiChu = new JLabel("Ghi Chú:");
		GridBagConstraints gbc_lblGhiChu = new GridBagConstraints();
		gbc_lblGhiChu.anchor = GridBagConstraints.WEST;
		gbc_lblGhiChu.insets = new Insets(0, 0, 0, 5);
		gbc_lblGhiChu.gridx = 1;
		gbc_lblGhiChu.gridy = 6;
		plNorth.add(lblGhiChu, gbc_lblGhiChu);
		
		txtGhiChu = new JTextField();
		GridBagConstraints gbc_txtGhiChu = new GridBagConstraints();
		gbc_txtGhiChu.gridwidth = 18;
		gbc_txtGhiChu.insets = new Insets(0, 0, 0, 5);
		gbc_txtGhiChu.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtGhiChu.gridx = 2;
		gbc_txtGhiChu.gridy = 6;
		plNorth.add(txtGhiChu, gbc_txtGhiChu);
		txtGhiChu.setColumns(10);
		
		lblPhieu = new JLabel("Phiếu");
		GridBagConstraints gbc_lblPhieu = new GridBagConstraints();
		gbc_lblPhieu.anchor = GridBagConstraints.WEST;
		gbc_lblPhieu.insets = new Insets(0, 0, 0, 5);
		gbc_lblPhieu.gridx = 21;
		gbc_lblPhieu.gridy = 6;
		plNorth.add(lblPhieu, gbc_lblPhieu);
		
		txtPhieu = new JTextField();
		GridBagConstraints gbc_txtPhieu = new GridBagConstraints();
		gbc_txtPhieu.insets = new Insets(0, 0, 0, 5);
		gbc_txtPhieu.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPhieu.gridx = 22;
		gbc_txtPhieu.gridy = 6;
		plNorth.add(txtPhieu, gbc_txtPhieu);
		txtPhieu.setColumns(11);
	
		plCenter = new JPanel();		
		add(plCenter,BorderLayout.CENTER);
		plCenter.setLayout(new BorderLayout());
    	JScrollPane scrollPane = new JScrollPane();
    	pMenu = new MenuAction_MuaHang(trangchu);
    	scrollPane.setComponentPopupMenu(pMenu);
    	plCenter.add(scrollPane);
		
    	model = new DefaultTableModel(data, colunms);
    	table = new JTable(model);
    	table.addPropertyChangeListener(new PropertyChangeListener() {
    		public void propertyChange(PropertyChangeEvent arg0) {
    			getInfo();
    		}
    	});
    	   	
    	table.getColumnModel().getColumn(1).setPreferredWidth(250);
		table.getColumnModel().getColumn(2).setPreferredWidth(150);
		table.getColumnModel().getColumn(7).setMaxWidth(0);
		table.getColumnModel().getColumn(7).setMinWidth(0);
		table.getColumnModel().getColumn(7).setPreferredWidth(0);
    	scrollPane.setViewportView(table);

    	
		plSouth = new JPanel();
		plSouth.setLayout(new BorderLayout());
		add(plSouth,BorderLayout.SOUTH);
		JPanel plSouth1 = new JPanel();
		plSouth1.setLayout(new BorderLayout());
		plSouth.add(plSouth1,BorderLayout.CENTER);
		JPanel plbuttom = new JPanel();
		plbuttom.setLayout(new FlowLayout(FlowLayout.LEFT));
		plSouth1.add(plbuttom,BorderLayout.WEST);		
		btnThem = new JButton("Thêm",new ImageIcon("images/them1.gif"));	
		plbuttom.add(btnThem);
		btnSua = new JButton("Sửa",new ImageIcon("images/sua1.gif"));	
		plbuttom.add(btnSua);
		btnXoa = new JButton("Xóa",new ImageIcon("images/xoa1.gif"));	
		plbuttom.add(btnXoa);
		btnTaoMoi = new JButton("Tạo Mới",new ImageIcon("images/new1.gif"));
		plbuttom.add(btnTaoMoi);
		
		JPanel pltxtTong = new JPanel();
		plbuttom.setLayout(new FlowLayout(FlowLayout.RIGHT));
		plSouth1.add(pltxtTong,BorderLayout.EAST);
		
		JLabel lblTong = new JLabel("Thành Tiền: ");
		pltxtTong.add(lblTong);
		txtTong = new JTextField(15);
		pltxtTong.add(txtTong);
		txtTong.setEditable(false);
		
		JLabel lblDaTra = new JLabel("--- Đã Trả: ");
		pltxtTong.add(lblDaTra);
		txtDaTra = new JTextField(15);
		txtDaTra.setText("0");
		txtDaTra.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				if(CheckString.OnlyNumber(txtDaTra.getText(), 9, 1)==true){
					getInfoConLai();
					txtDaTra.setBackground(Color.white);
				}else{
					JOptionPane.showMessageDialog(txtDaTra, "Lỗi nhập!", "Lỗi", JOptionPane.OK_OPTION);
					txtDaTra.setBackground(Color.red);
				}
			}
		});
		pltxtTong.add(txtDaTra);
		
		txtDaTra.setEditable(false);
		
		
		JLabel lblConLai = new JLabel("==> Còn Lại: ");
		pltxtTong.add(lblConLai);
		txtConLai = new JTextField(15);
		pltxtTong.add(txtConLai);
		txtConLai.setEditable(false);
		txtConLai.setText("0");
		
		JPanel plSouth2 = new JPanel();
		plSouth.add(plSouth2,BorderLayout.SOUTH);		
		plSouth2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		JLabel lblgoithieu = new JLabel("Phần mềm quản lý bán hàng ShopM                ");
		lblgoithieu.setFont(new Font("Tahoma", 0, 10));
		lblgoithieu.setFont(new Font("Tahoma", 3, 11));		
		lblgoithieu.setForeground(Color.blue);
		plSouth2.add(lblgoithieu);
		final JLabel lblgioserver = new JLabel();
		plSouth2.add(lblgioserver);
		 final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a | dd/MM/yyyy");
	        Timer time = new Timer(1000, new ActionListener()
	        {
	            public void actionPerformed(ActionEvent e)
	            {
	                Date d = new Date(System.currentTimeMillis());
	                lblgioserver.setText(sdf.format(d.getTime()));
	            }
	        });
	        time.start();
	        
	        //enabled buttom     
			
	        int i = model.getRowCount();
			if(i==0){
				btnSua.setEnabled(false);
		        btnXoa.setEnabled(false);
		        btnTaoMoi.setEnabled(false);
			}
			
	        //dang ky su kien
	        btnThem.addActionListener(this);
	        btnSua.addActionListener(this);
	        btnXoa.addActionListener(this);
	        btnTaoMoi.addActionListener(this);
	        cmbMa.addActionListener(this);
	        cmbTenNPP.addActionListener(this);
	        cmbThanhToan.addActionListener(this);
	        cbXacNhanPP.addActionListener(this);
	        
	        DbselectNhaPP();
	        DbselectDieuKhoan();
	        DbselectNhanVien();	
	        DBphieu();
    }
	
	//hiển thị nhà phân phối
	
	public void DbselectNhaPP(){
		DSJ_NPPMuaHang data = new DSJ_NPPMuaHang();
		Vector<DSJ_NPPMuaHang> getNPPMuaHang = data.getUsers();
		for(int i=0;i<getNPPMuaHang.size();i++){
			cmbMa.addItem(getNPPMuaHang.get(i).getMaCC());
			cmbTenNPP.addItem(getNPPMuaHang.get(i).getNhaCungCap());
			txtDiaChi.setText(getNPPMuaHang.get(0).getDiaChi());
			txtDienThoai.setText(getNPPMuaHang.get(0).getDienThoai());
		}
	}
	//hiển thị tên nhân viên
	public void DbselectNhanVien(){
		DSJ_NhanVien data = new DSJ_NhanVien();
		Vector<DSJ_NhanVien> getnhanvien = data.getUsers();
		for(int i=0;i<getnhanvien.size();i++){
			cmbTenNV.addItem(getnhanvien.get(i).getHoTenNV());
		}
	}
	//hiển thị phieu

	public void DBphieu(){
		//tang so luong
		DSJ_FormMuaHang data = new DSJ_FormMuaHang();
		String getMuaHangs = data.selectmaNH(data);
		if(getMuaHangs==null){
			txtPhieu.setText("NH100001");
		}
		else{
			Integer n1 = Integer.parseInt(getMuaHangs);
			Integer n2 = 100001;
			int t = n1+n2;
			String b = String.valueOf(t); 
			txtPhieu.setText("NH"+b);
		}
	}
	
	//hiển thi điều khoản thanh toan
	public void DbselectDieuKhoan(){
		DSJ_DieuKhoanThanhToan data = new DSJ_DieuKhoanThanhToan();
		Vector<DSJ_DieuKhoanThanhToan> getDieuKhoan = data.getDieuKhoan();
		for(int i=0;i<getDieuKhoan.size();i++){
			cmbThanhToan.addItem(getDieuKhoan.get(i).getTenDK());
		}
	}
	
	public void getInfo(){
		Integer a =0;
		for (int i = 0; i < table.getModel().getRowCount(); i++) {
			a = a +Integer.parseInt((String) table.getModel().getValueAt(i, 6));
		}
		txtTong.setText(a.toString());
	}
	//Con lai
	public void getInfoConLai(){
		Integer a =0;
		for (int i = 0; i < table.getModel().getRowCount(); i++) {
			a = a +Integer.parseInt((String) table.getModel().getValueAt(i, 6));
		}
		String c = txtDaTra.getText();
		Integer n2 = Integer.parseInt(c);
		int a1 = a-n2;
		String d = String.valueOf(a1); 
		txtConLai.setText(d);	
	}
	//getinfo
	public void bindDataToForm(FormUpdateMuaHang updatemuahang){
		int row = this.table.getSelectedRow();
		updatemuahang.cmbMaHang.setSelectedItem(this.table.getModel().getValueAt(row, 0).toString());
		updatemuahang.cmbTenHang.setSelectedItem(this.table.getModel().getValueAt(row, 1).toString());
		updatemuahang.txtKhoHang.setText(this.table.getModel().getValueAt(row, 2).toString());
		updatemuahang.txtDonVi.setText(this.table.getModel().getValueAt(row, 3).toString());
		updatemuahang.txtDonGia.setText(this.table.getModel().getValueAt(row, 4).toString());
		updatemuahang.txtSoLuong.setText(this.table.getModel().getValueAt(row, 5).toString());
		updatemuahang.txtThanhTien.setText(this.table.getModel().getValueAt(row, 6).toString());

	}
	
	public void deleteAllRows( DefaultTableModel model ){
		for( int i = model.getRowCount() - 1; i >= 0; i-- )
		{
			model.removeRow(i);
		}
	}
	
	//actionPerformed

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cbXacNhanPP){
			int xoa = JOptionPane.showConfirmDialog(cbXacNhanPP,  "Bạn đã chắc chọn đúng nhà cung cấp cần giao dịch?\nKích vào nút Tạo Mới để kết thúc giao dịch này.", "", JOptionPane.YES_NO_OPTION);
			if(xoa == JOptionPane.YES_OPTION){
				cmbTenNPP.setEnabled(false);
				cmbMa.setEnabled(false);
				cbXacNhanPP.setEnabled(false);	
				DBphieu();
			}else{
				cbXacNhanPP.setSelected(false);
			}
		}
		if(e.getSource()==cmbThanhToan){
			if(cmbThanhToan.getSelectedItem().toString().equals("Thanh Toán Ngay") ){
				txtDaTra.setEditable(false);
				Date todayD=new Date(System.currentTimeMillis());
				datehanthanhtoan.setDate(todayD);
				datehanthanhtoan.setEnabled(false);
			}
			if(cmbThanhToan.getSelectedItem().toString().equals("Công Nợ")){
				txtDaTra.setEditable(true);
				datehanthanhtoan.setEnabled(true);				
			}
		}
		if(e.getSource()==cmbMa){
			try {
			       String sql = "select * from NhaPhanPhoi WHERE MaCC = '" +cmbMa.getSelectedItem().toString() + "'";
			        st = con.prepareStatement(sql);
					ResultSet rs = st.executeQuery();
			        while(rs.next()){
			        	cmbTenNPP.setSelectedItem(rs.getString("NhaCungCap"));
			        	txtDiaChi.setText(rs.getString("DiaChi"));
			        	txtDienThoai.setText(rs.getString("DienThoai"));
			        }
			                       
			    } catch (SQLException ex) {
			       ex.printStackTrace();
			    }
			
		}
		if(e.getSource()==cmbTenNPP){
			try {
			       String sql = "select * from NhaPhanPhoi WHERE NhaCungCap = N'" +cmbTenNPP.getSelectedItem().toString() + "'";
			        st = con.prepareStatement(sql);
					ResultSet rs = st.executeQuery();
			        while(rs.next()){
			        	cmbMa.setSelectedItem(rs.getString("MaCC"));
			        	txtDiaChi.setText(rs.getString("DiaChi"));
			        	txtDienThoai.setText(rs.getString("DienThoai"));
			        }
			                       
			    } catch (SQLException ex) {
			       ex.printStackTrace();
			    }
			
		}
		if(e.getSource() == btnTaoMoi){		
			String soluong = txtTong.getText();
			Integer intsoluong = Integer.parseInt(soluong);
			String datra = txtDaTra.getText();
			Integer intdatra = Integer.parseInt(datra);
			if(intsoluong<intdatra){
				JOptionPane.showMessageDialog(btnTaoMoi, "Vui lòng nhập lại!", "Lỗi Đã Trả", JOptionPane.OK_OPTION);
				txtDaTra.setBackground(Color.red);
			}else{		
				deleteAllRows(model);
				btnSua.setEnabled(false);
		        btnXoa.setEnabled(false);
		        btnTaoMoi.setEnabled(false);
		        
		        DSJ_FormMuaHang delegate = new DSJ_FormMuaHang();
		        DSJ_FormMuaHang muahang = new DSJ_FormMuaHang();
		        DSJ_NPPMuaHang nhapp = new DSJ_NPPMuaHang();
		        
		        nhapp.setMaCC(cmbMa.getSelectedItem().toString());
		        nhapp.setNhaCungCap(cmbTenNPP.getSelectedItem().toString());
				java.util.Date datengay =  dateStart.getDate();
				String datengaynhaphang = String.format("%1$td-%1$tm-%1$tY", datengay);
				muahang.setNgayNhapHang(datengaynhaphang);
				muahang.setChungTu(txtPhieu.getText());
				muahang.setGhiChu(txtGhiChu.getText());
				muahang.setTongThanhToan(txtTong.getText().toString());
				muahang.setDaTra(txtDaTra.getText().toString());
				muahang.setConLai(txtConLai.getText().toString());
				muahang.setLoaiChungTu(title.getTitle().toString());
				
				delegate.insertGiaoDichMuaHang(muahang, nhapp);
		        
		        cbXacNhanPP.setEnabled(true);
		        cbXacNhanPP.setSelected(false);
				cmbTenNPP.setEnabled(true);
				cmbMa.setEnabled(true);		
				txtDaTra.setBackground(Color.white);
				txtTong.setText("0");
		        txtDaTra.setText("0");
		        txtConLai.setText("0");
		        txtTong.setText("0");
			}
		}
		if(e.getSource() == btnThem){
			
			if(cbXacNhanPP.isSelected()==false){
				JOptionPane.showMessageDialog(btnThem, "Vui lòng xác nhận nhà phân phối cần giao dịch.", "Lỗi Xác Nhận", JOptionPane.OK_OPTION);		
			}else{
				new FormMuaHang(this).setVisible(true);
			}
			
		}
		if(e.getSource() == btnSua){
			updatemuahang =new FormUpdateMuaHang(this);			
			bindDataToForm(updatemuahang);
			updatemuahang.setVisible(true);
			
			
		}
		if(e.getSource() == btnXoa){
			int xoa = JOptionPane.showConfirmDialog(btnXoa,  "Bạn có chắc muốn xóa không?", "Delete", JOptionPane.YES_NO_OPTION);
			if(xoa == JOptionPane.YES_OPTION){
				int row = this.table.getSelectedRow();
				DSJ_FormMuaHang data = new DSJ_FormMuaHang();
				FormMuaHang frmbanhang = new FormMuaHang(frame);
				data.setMaSP(frmbanhang.cmbMaHang.getSelectedItem().toString());
				String getMuaHangs = data.selecthanghoa(data);
				
				Integer n1 = Integer.parseInt(getMuaHangs);
				String s= this.table.getModel().getValueAt(row, 5).toString();
				Integer n3 = Integer.parseInt(s);
				int tong = n1-n3;
				String d = String.valueOf(tong); 
				
				data.setSoLuong(d);
				data.updatehanghoa(data);
				
				String id = this.table.getModel().getValueAt(row, 7).toString();
				DSJ_FormMuaHang b = new DSJ_FormMuaHang();
				//b.setNhapHang_ID(id);
				b.del(id);
				model.removeRow(row);
				getInfoConLai();
				
				int i = model.getRowCount();
				if(i==0){
					btnSua.setEnabled(false);
			        btnXoa.setEnabled(false);
			        btnTaoMoi.setEnabled(false);
			        
			        cbXacNhanPP.setEnabled(true);
					cbXacNhanPP.setSelected(false);
					cmbTenNPP.setEnabled(true);
					cmbMa.setEnabled(true);
			        
			        txtTong.setText("0");
			        txtDaTra.setText("0");
			        txtConLai.setText("0");
				}
			}
			
		}		
	}
}
